import java.lang.Thread;
import java.util.ArrayList;

import javax.swing.*;

public class Fenetre2 extends Thread{

    private String derniereCommande;
    
    public Fenetre2(){
    	JFrame fen = new JFrame();
    	fen.setBounds(0,0,100,100);
    	fen.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    	fen.setVisible(true);
    	run();
    	
    	
    }

    public void lancerNouvelleAction(Action lAction){
    	String programme = null;
	JOptionPane jop = new JOptionPane();
	ImageIcon img = null;
	ImageIcon imgInternet = new ImageIcon("/home/egenerat/IE6.png");
	ImageIcon imgSms = new ImageIcon("/home/egenerat/sms.png");			
	ImageIcon imgTelephone = new ImageIcon("/home/egenerat/phone.png");
	ImageIcon imgNote = new ImageIcon("/home/egenerat/note.png");
	ImageIcon imgWifi = new ImageIcon("/home/egenerat/wifi.png");
	String typeAction = lAction.getNom();
	if(typeAction.equals("Internet")){
		img = imgInternet;
		programme = "firefox " +lAction.getNIemeArg(1);
	}
	if(typeAction.equals("SMS")){
		img = imgSms;
	}
	if(typeAction.equals("Telephone")){
		img = imgTelephone;
	}
	if(typeAction.equals("Texte")){
		img = imgNote;
	}
	if(typeAction.equals("WIFI")){
		img = imgWifi;
	}
	int option = jop.showConfirmDialog(null, "Voulez-vous lancer l'application ?", "Lancement de l'animation", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, img);
	if(option == JOptionPane.OK_OPTION)
	    System.out.println("on lance le programme");
	else
	    System.out.println("on annule");
	try{
		System.out.println("Commande :"+programme);
	    Process p = Runtime.getRuntime().exec(programme);
	}
	catch(Exception e){
	    System.out.println("Le programme associé n'est pas installé");
	}
    }

    public void run(){
	while(true){
	    String derniereCommandeDuFichier = Toolbox.recupererDerniereCommande();
	    if (derniereCommandeDuFichier != derniereCommande){
			derniereCommande = derniereCommandeDuFichier;
			Action lAction = Toolbox.recupererActionAssociee(derniereCommandeDuFichier);
			lancerNouvelleAction(lAction);
	    }
	    try{
	    	sleep(100);
	    }
	    catch(Exception e){}
	}
    }
}